<?php
class ApiCenter_SalesApi_OauthController extends Mage_Core_Controller_Front_Action {
   
	public function indexAction() {
// 		echo "please check your url!";
// 	   $data = Mage::helper("salesapi")->_businessFilterOrder("137","12");
// 	   print_r($data);
	}
	public  function tocheckAction(){
	    return "";
	    exit;
	    $helper = Mage::helper('oauth');
	    
	    $server = Mage::getModel('oauth/server');
	    $token = $server->authorizeToken('1', Mage_Oauth_Model_Token::USER_TYPE_ADMIN);
	    $CallbackUrl = $helper->getFullCallbackUrl($token);
	    $this->_redirectUrl($CallbackUrl);
	}

	
	public function oauthAuthorizeAction(){
	    $result = array();
         try{
	    /** @var $helper Mage_Oauth_Helper_Data */
	    $server = Mage::getModel('oauth/server');
	    $token = $server->authorizeToken('1', Mage_Oauth_Model_Token::USER_TYPE_ADMIN);
	    if(!$token->getAuthorized()){
	        Mage::throwException('Token is not authorized');
	     }
	       $result['status'] ="1";
	       $result['data']['oauth_token']=$token->getToken();
	       $result['data']['verifier']=$token->getVerifier();
         }catch(Exception $e){
             $result['status'] ="0";
             $result['data']['error']=$e->getMessage();
         }
         echo json_encode($result);
	}
	public function autoRefreshTokenAction(){
	    $callbackUrl=Mage::getBaseUrl()."salesapi/oauth/autorefreshtoken";
	    $temporaryCredentialsRequestUrl = Mage::getBaseUrl()."oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
	    $adminAuthorizationUrl = Mage::getBaseUrl().'salesapi/oauth/oauthAuthorize'; //请求授权
	    $accessTokenRequestUrl = Mage::getBaseUrl().'oauth/token';//请求授权
	    $dataInfo =  $this->getRequest()->getPost();
	    $consumerKey =$dataInfo['key'] ;
	    $consumerSecret = $dataInfo['secret'];
		
	    try {  
                    $authType = @OAUTH_AUTH_TYPE_URI;  
                    $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);  
                    $oauthClient->disableSSLChecks();
                    
                    $oauthClient->enableDebug();  
				
                    if (!@$_SESSION['state']) {  
                        $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);  
                        $_SESSION['secret'] = $requestToken['oauth_token_secret'];  
                        //print_r($_SESSION);exit;     
                         //初始化
                        $ch =curl_init();
                        curl_setopt($ch, CURLOPT_URL, $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        $output = json_decode(curl_exec($ch),true);      
                        curl_close($ch);
                        if($output){
								
                                $oauth_token =$output['data']['oauth_token']; 
                                $Verifier         = $output['data']['verifier'];
                                $oauthClient->setToken($oauth_token, $_SESSION['secret']);
                                $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl,"",$Verifier);
                                if(empty($accessToken)) {
                                    exit("Failed fetching access token, response was: " . $oauthClient->getLastResponse());
                                }
                                $_SESSION['token'] = $accessToken['oauth_token'];
                                $_SESSION['secret'] = $accessToken['oauth_token_secret'];
								$result['status'] ="1";
                                $result['data']['oauth_token']= $_SESSION['token'] ;
                                $result['data']['oauth_token_secret']=$_SESSION['secret'];
                                @$_SESSION['state'] == 2;      
                        }else{
                              $result['status'] ="0";
                              $result['data']['error']="curl error";
                              echo json_encode($result);
                        }
                
                    }elseif (@$_SESSION['state'] == 2){
                        $result['status'] ="1";
                        $result['data']['oauth_token']= $_SESSION['token'] ;
                        $result['data']['oauth_token_secret']=$_SESSION['secret'];
                    }
	
                } catch (OAuthException $e) {
					unset($_SESSION['state']);
                    $result['status'] ="0";
                    $result['data']['error']=$e->getMessage();
                 }catch(Exception $e ){
					 unset($_SESSION['state']);
                     $result['status'] ="0";
                     $result['data']['error']=$e->getMessage();
                  }
                  echo json_encode($result);
         }
}